<#if debug>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<script type="text/javascript" src="../js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="../js/jquery.tol.js"></script>
<link rel="stylesheet" href="../front_themes/default/tol.multi.css" type="text/css" />
</head>
<body>
</#if>
<div class="tol-multi-div" >${action.getText('page.title')}<a href="javascript:void(0);" class="tol-close" tol="tol-close-${key}">&nbsp;</a>
	<div class="tol-multi-select">
		<div class="tol-multi-back-up">
			<a href="javascript:void(0);" tol="tol-multi-tree-a-back-up-${key}" >${action.getText('page.backUp')}</a>
		</div>
		<div class="tol-multi-tree-current-level" >
			${action.getText('page.currentLevel')}:<font tol="tol-multi-tree-current-show-${key}">${action.getText('page.rootName')}</font>
		</div>
		<#if (clear>0) >
			<div class="tol-multi-clear-btn" ><a tol="tol-clear-func-${key}" href="javascript:void(0);">${action.getText('page.clearUp')}</a></div>
		</#if>
		<#if (root>0) >
			<div class="tol-multi-canroot-btn" ><a tol="tol-clear-canroot-${key}" href="javascript:void(0);">${action.getText('page.root')}</a></div>
		</#if>
	</div>
	<input type="hidden" tol="tol-multi-hide-div-first-${key}" value="true" />
	<div class="tol-multi-item" tol="tol-multi-div-items-${key}">
		<#--数据 -->
	</div>
	<div class="tol-clear" ></div>
	<div class="tol-multi-separate" >
	<#if (multi>0) >
		<div>${action.getText('page.title.currentSelects')}</div>
		<div class="tol-multi-choose" tol="tol-multi-hide-div-my-choose-${key}" >
		<#-- 选中的结果集 -->
		</div>
	</#if>
	<#if (pagination>0) >
		<div class="tol-multi-pagination" tol="tol-multi-pagination-${key}" >
			<a href="javascript:void(0)" tol="tol-multi-pagination-prev-${key}" >&lt;&lt;</a>
			<span class="tol-multi-pagenavselected" tol="tol-multi-pagenavselected-${key}" >${page}</span>
			<a href="javascript:void(0)" tol="tol-multi-pagination-next-${key}" >&gt;&gt;</a>
		</div>
	</#if>	
	<a tol="tol-multi-hide-add-btn-${key}" class="tol-multi-add-btn" href="javascript:void(0);" title="${action.getText('page.confirm')}" >
		<span>${action.getText('page.confirm')}</span>
	</a>
	</div>
</div>
<script type="text/javascript" >

$(function(){
	var tol_multi_${key} = {};
	tol_multi_${key}.selects = [];
	
	<#if (clear>0) >
	$("a[tol='tol-clear-func-${key}']").live('click',function(){
		var v = $(options.target).data("options");
		tol_multi_${key}.ids = [];
		tol_multi_${key}.texts = [];
		$('#tol-multi-hide-div-my-choose-'+v.key).empty();
		$("input[id^='tol-multi-hide-div-item-checkbox-"+v.key+"-']").attr('checked','');
	});
	</#if>
	
	<#if (root>0) >
	$("tol='tol-clear-canroot-${key}'").live('click',function(){
		options.canRoot.success(options);
		close(options);
	});
	</#if>
	
	$("a[tol='tol-multi-tree-a-back-up-${key}']").live('click',function(){
		back();
	});
	
	$("a[tol='tol-multi-pagination-prev-${key}']").live('click',function(){
		toPrev();
	});
	
	$("a[tol='tol-multi-pagination-next-${key}']").live('click',function(){
		toNext();
	});
	
	load('${id}','${entity.text}');
	
	function removeChoose(id){//移除选项
		tol_multi_${key}.selects = $.grep(tol_multi_${key}.selects, function(n,i){
			return n.id!=id;
		});
		$("#tol-multi-hide-div-item-checkbox-${key}-"+id).attr('checked','');
		showMyChoose();
	}
	
	function recheckChoose(){//重新将选中的checkbox项打钩
		$.each(tol_multi_${key}.selects,function(i,data){
			$("#tol-multi-hide-div-item-checkbox-${key}-"+data.id).attr('checked',true);
		});
	}
	
	function showMyChoose(){
		$("div[tol='tol-multi-hide-div-my-choose-${key}']").empty();
		$.each(tol_multi_${key}.selects,function(i,data){
			var a = $("<a>", {
			  	tol:'tol-multi-hide-div-my-choose-item-${key}-'+data.id,
			  	href:'javascript:void(0)',
			  	'class':'tol-multi-my-choose-a',
			  	html:data.text
			});
			a.bind('click',function(){return removeChoose(data.id)});
			$("div[tol='tol-multi-hide-div-my-choose-${key}']").append(a);
		});
	}
	
	function load(id,text,page){
			var reval = true;
			page = page?page:1;
			//var width = ($("tol='tol-multi-div-items-${key}'").width()-(options.rowcount*10))/options.rowcount; 
			$.ajax({
					<#if (remotePage>0)>
					url:'tree-view!page.htm',
					<#else>
					url:'tree-view!load.htm',
					</#if>
					data:{k:'${k}',id:id,page:page,rp:${rp} },
					type:'POST',
					dataType :'json',
					async:false,
					success : function(json){
						var list = json;
						tol_multi_${key}.selects.currentId = id;
						tol_multi_${key}.selects.currentText = text;
						<#if (remotePage>0)>
						list = json.rows;
						</#if>
						if($.isEmptyObject(list) || list.length==0){
							reval = false;
							alert("${action.getText('page.noChild')}");
							return;
						}
						$("div[tol='tol-multi-div-items-${key}']").empty(); 
						
						$.each(list, function(i, data){
							var html ='<div tol="tol-multi-item-label-${key}-'+i+'" class="tol-multi-item-label ';
							<#if (pagination>0) >
							if(i>=${rp}){
								html +='hidden';		
							}
							</#if>
							
							html +='" title="'+data.text+'" style="width:100px" >';
							
							<#if (multi>0) >
							html +='<input type="checkbox" id="tol-multi-hide-div-item-checkbox-${key}-'+data.id+'" />';
							</#if>
							html +='<a style="cursor:pointer" tol="tol-multi-hide-div-item-${key}-'+data.id+'" href="javascript:void(0)">'+data.text.cutOf(8)+'&nbsp;</a></div>'
							$("div[tol='tol-multi-div-items-${key}']").append(html);
							$("a[tol='tol-multi-hide-div-item-${key}-"+data.id+"']").click(function(){//显示下一层
								if(load(data.id,data.text)){
									tol_multi_${key}.backId =id;
									tol_multi_${key}.backText =text;
								} 
							});
							<#if (multi>0) >
							$("#tol-multi-hide-div-item-checkbox-${key}-"+data.id).click(function(){//绑定checkbox点击事件，绑定点击事件
								if(!this.checked){//移除操作
									removeChoose(data.id);
									return;
								}
								
								if(tol_multi_${key}.selects.length >=${maxCount}){
									$(this).attr('checked','');
									alert("${action.getText('page.over.maxCount',[maxCount?string ,"${action.getText('page.showName')}"])}");
									return;
								}
								
								
								for(var i=0;i<tol_multi_${key}.selects.length;i++){
									if(tol_multi_${key}.selects[i].id==data.id){
										$(this).attr('checked','');
										alert("${action.getText('page.alert.haveChoose',["${action.getText('page.showName')}"])}");
										return;
									}
								}
								
								tol_multi_${key}.selects.push(data);
								
								showMyChoose();
							});
							</#if>
							
						});
						<#if (pagination>0) >
							var sum =1;
							var page = 1;
							<#if (remotePage>0) >
							sum = json.sum;
							page = json.page
							<#else>
							sum = parseInt((list.length+(${rp}-1))/${rp});
							</#if>
							showPage(page,sum);
						</#if>
						<#if (multi>0) >
							recheckChoose();
						</#if>
					}
			});	
			return reval;
	}
	
	function back(){
		load(tol_multi_${key}.backId,tol_multi_${key}.backText);
	}
	
	function showPage(current,sum){
		tol_multi_${key}.currentPage = current;
		tol_multi_${key}.sumPage = sum?sum:tol_multi_${key}.sumPage;
		$("span[tol='tol-multi-pagenavselected-${key}']").empty();
		$("span[tol='tol-multi-pagenavselected-${key}']").html(current+"/"+tol_multi_${key}.sumPage);
	}
	
	function toPrev(){
		var page = tol_multi_${key}.currentPage;
		if(page==1){
			return;
		}
	<#if (remotePage>0) >
		load(tol_multi_${key}.selects.currentId,tol_multi_${key}.selects.currentText,page-1);
	<#else>
		$("div[tol^='tol-multi-item-label-${key}-']").addClass('hidden');
		var first = (page-2)*${rp};
		var end = (page-1)*${rp}-1;
		for(var i=first;i<=end;i++){
			$("div[tol='tol-multi-item-label-${key}-"+i+"']").removeClass('hidden');
		}
		showPage(page-1);	
	</#if>
	}
	
	function toNext(){
		var page = tol_multi_${key}.currentPage;
		var sum = tol_multi_${key}.sumPage;
		if(page==sum){
			return;
		}
	<#if (remotePage>0) >
		load(tol_multi_${key}.selects.currentId,tol_multi_${key}.selects.currentText,page+1);
	<#else>
		$("div[tol^='tol-multi-item-label-${key}-']").addClass('hidden');
		var first = page*${rp};
		var end = (page+1)*${rp}-1;
		for(var i=first;i<=end;i++){
			$("div[tol='tol-multi-item-label-${key}-"+i+"']").removeClass('hidden');
		}
		showPage(page+1);	
	</#if>
	}
	
});

</script>
<#if debug>
</html>
</body>
</#if>
